using System;

namespace Acme.BookStore.Redis
{
    /// <summary>
    /// 客户数据缓存键管理类
    /// </summary>
    public static class KehuCacheKeys
    {
        /// <summary>
        /// 客户缓存键前缀
        /// </summary>
        public const string Prefix = "BookStore:Kehu:";
        
        /// <summary>
        /// 单个客户数据缓存键
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns>缓存键</returns>
        public static string ForKehu(string id) => $"{Prefix}id:{id}";
        
        /// <summary>
        /// 客户列表数据缓存键
        /// </summary>
        /// <param name="skipCount">跳过数量</param>
        /// <param name="maxResultCount">最大结果数</param>
        /// <param name="sorting">排序</param>
        /// <returns>缓存键</returns>
        public static string ForKehuList(int skipCount, int maxResultCount, string sorting)
            => $"{Prefix}list:{skipCount}:{maxResultCount}:{sorting ?? "Name"}";
        
        /// <summary>
        /// 客户名称搜索缓存键
        /// </summary>
        /// <param name="nameKeyword">名称关键词</param>
        /// <param name="skipCount">跳过数量</param>
        /// <param name="maxResultCount">最大结果数</param>
        /// <param name="sorting">排序</param>
        /// <returns>缓存键</returns>
        public static string ForKehuNameSearch(string nameKeyword, int skipCount, int maxResultCount, string sorting)
            => $"{Prefix}nameSearch:{nameKeyword}:{skipCount}:{maxResultCount}:{sorting ?? "Name"}";
        
        /// <summary>
        /// 客户高级搜索缓存键
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <param name="name">名称</param>
        /// <param name="contactPerson">联系人</param>
        /// <param name="phone">电话</param>
        /// <param name="mobile">手机</param>
        /// <param name="enterpriseAttribute">企业属性</param>
        /// <param name="skipCount">跳过数量</param>
        /// <param name="maxResultCount">最大结果数</param>
        /// <param name="sorting">排序</param>
        /// <returns>缓存键</returns>
        public static string ForKehuAdvancedSearch(
            string filter,
            string name,
            string contactPerson,
            string phone,
            string mobile,
            string enterpriseAttribute,
            int skipCount,
            int maxResultCount,
            string sorting)
        {
            // 使用哈希算法生成缓存键，避免过长的键名
            var searchParameters = $"{filter}|{name}|{contactPerson}|{phone}|{mobile}|{enterpriseAttribute}";
            var paramHash = string.Concat(searchParameters.GetHashCode().ToString("x"));
            return $"{Prefix}advSearch:{paramHash}:{skipCount}:{maxResultCount}:{sorting ?? "Name"}";
        }
        
        /// <summary>
        /// 客户总数缓存键
        /// </summary>
        public static string ForKehuCount() => $"{Prefix}count";
        
        /// <summary>
        /// 客户名称搜索总数缓存键
        /// </summary>
        /// <param name="nameKeyword">名称关键词</param>
        /// <returns>缓存键</returns>
        public static string ForKehuNameSearchCount(string nameKeyword) 
            => $"{Prefix}nameSearchCount:{nameKeyword}";
        
        /// <summary>
        /// 客户高级搜索总数缓存键
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <param name="name">名称</param>
        /// <param name="contactPerson">联系人</param>
        /// <param name="phone">电话</param>
        /// <param name="mobile">手机</param>
        /// <param name="enterpriseAttribute">企业属性</param>
        /// <returns>缓存键</returns>
        public static string ForKehuAdvancedSearchCount(
            string filter,
            string name,
            string contactPerson,
            string phone,
            string mobile,
            string enterpriseAttribute)
        {
            var searchParameters = $"{filter}|{name}|{contactPerson}|{phone}|{mobile}|{enterpriseAttribute}";
            var paramHash = string.Concat(searchParameters.GetHashCode().ToString("x"));
            return $"{Prefix}advSearchCount:{paramHash}";
        }
    }
} 